<template>
  <base-modal :title="$t('personal.updateMainTag.title')" :show.sync="showUpdateDialog" width="400px">
    <div class="base-modal-content">
      <!-- 内容主体区域 -->
      <el-select v-model="mainTagId" :placeholder="$t('common.placeholder.selectMainDepartment')" @input="update">
        <el-option
          v-for="item in tags"
          :key="item.id"
          :label="item.tagName"
          :value="item.id"
        ></el-option>
      </el-select>
    </div>
    <div slot="footer" class="dialog-footer">
      <el-button @click="showUpdateDialog = false">{{ $t('common.base.cancel') }}</el-button>
      <el-button type="primary" @click="save" :disabled="pending">{{ $t('common.base.confirm') }}</el-button>
    </div>
  </base-modal>
</template>

<script>
import { t } from '@src/locales'

export default {
  name: "update-main-tag",
  data() {
    return {
      pending: false,
      showUpdateDialog: false,
      mainTagId: "",
      tags: [],
    };
  },
  methods: {
    openDialog(user) {
      this.userId = user.userId;
      this.tags = user.tagList || [];

      // 当主部门被删除，不展示之前的主部门id
      this.tags.map((tag) => {
        if (tag.id === user.mainTeamId) {
          this.mainTagId = user.mainTeamId;
        }
      });
      this.showUpdateDialog = true;
    },
    update(value) {
      this.mainTagId = value;
    },
    async save() {
      try {
        this.pending = true;
        const params = {};
        params.mainTagId = this.mainTagId;
        params.userId = this.userId;
        const { status, message } = await this.$http.post(
          "/security/user/setMainTeam",
          params,
          false
        );
        this.pending = false;
        if (status !== 0)
          return this.$platform.notification({
            title: t('common.base.tip.modifyFail'),
            message: message || "",
            type: "error",
          });
        this.$platform.notification({
          title: t('common.base.tip.modifySuccess'),
          type: "success",
        });
        this.$emit("update");
        this.showUpdateDialog = false;
      } catch (e) {
        console.error(e);
        this.pending = false;
      }
    },
  },
};
</script>

<style lang="scss" scoped>
::v-deep.el-dialog__headerbtn {
  top: 0 !important;
}
.base-modal-content {
  padding: 20px 20px 30px;
  .el-select {
    width: 100%;
  }
}
</style>
